package com.Calculator.view.Panel.impl;

import com.Calculator.controller.DisplayText.KeyListener;
import com.Calculator.models.impl.Theme;
import com.Calculator.view.TextField.impl.DisplayText;
import com.Calculator.view.TextField.impl.ProcessText;
import com.Calculator.view.Panel.iMyJPanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

public class TextPanel implements iMyJPanel{
    //文本面板
    private JPanel textPanel;
    //DisplayText
    private DisplayText displayText;
    //ProcessText
    private ProcessText processText;

    public TextPanel(Theme theme) {
        //设置布局管理器
        textPanel = new JPanel(new BorderLayout());
        //设置大小
        textPanel.setPreferredSize(new Dimension(theme.getWidth(), theme.getHeight()*20/60));
        //设置边框
        textPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        //设置背景颜色
//        textPanel.setBackground(Color.red);
        //设置可聚焦
        textPanel.setFocusable(true);

        //添加文本框组件
        this.displayText = new DisplayText(theme);
        this.processText = new ProcessText(theme);
        textPanel.add(processText.getJTextField(),BorderLayout.NORTH);
        textPanel.add(displayText.getJTextField(),BorderLayout.CENTER);

        //焦点问题临时解决方法
        textPanel.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                System.out.println("Panel 获得焦点");
            }

            @Override
            public void focusLost(FocusEvent e) {
                System.out.println("Panel 失去焦点");
//                textPanel.requestFocus();
            }
        });


    }

    public DisplayText getDispalyText() {
        return displayText;
    }

    public void setDisplayText(DisplayText displayText) {
        this.displayText = displayText;
    }

    public ProcessText getProcessText() {
        return processText;
    }

    public void setProcessText(ProcessText processText) {
        this.processText = processText;
    }

    @Override
    public JPanel getJPanel() {
        return textPanel;
    }

    @Override
    public void setJPanel(JPanel jPanel) {
        this.textPanel = jPanel;
    }
}
